package com.ky.manage.permission;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.ky.manage.R;
import com.ky.manage.utils.ScreenUtils;
import java.lang.ref.WeakReference;


/**
 * dialog简单封装（目前用于权限弹框业务）
 *
 * @author zhaoyan@idstaff.com
 * @date 2019/03/27.
 */
public class PermissionDialog {
    private static final String TAG = PermissionDialog.class.getSimpleName();
    /**
     * 默认的对话框style
     */
    public static int DIALOG_COMMON_STYLE = R.style.WrapContentDialog;
    private final WeakReference<Context> mContextWeakReference;
    /**
     * 对话框style
     */
    private final int mDialogThemeResId;
    /**
     * 自定义的dialog
     */
    private Dialog mDialog;
    /**
     * 对话框view
     */
    private View mDialogView;

    public PermissionDialog(Context context, int dialogThemeResId) {
        mContextWeakReference = new WeakReference<>(context);
        mDialogThemeResId = dialogThemeResId;
    }

    /**
     * 重置dialog view
     */
    public void resetDialogView() {
        mDialogView = null;
    }

    /**
     * 设置对话框view布局
     *
     * @param layoutRes
     * @return
     */
    public PermissionDialog setDialogLayout(int layoutRes) {
        if (layoutRes <= 0 || null == mContextWeakReference) {
            return this;
        }
        Context context = mContextWeakReference.get();
        if (null == context) {
            return this;
        }
        mDialogView = LayoutInflater.from(context).inflate(layoutRes, null);
        //设置对话框style
        if (mDialogThemeResId <= 0) {
            mDialog = new Dialog(context, DIALOG_COMMON_STYLE);
        } else {
            mDialog = new Dialog(context, mDialogThemeResId);
        }
        // 调整dialog背景大小
        if (null != mDialogView) {
            int dialogWidth = (int) (ScreenUtils.getScreenW() * 0.8);
            mDialogView.setLayoutParams(new FrameLayout.LayoutParams(dialogWidth, FrameLayout.LayoutParams.WRAP_CONTENT));
            mDialog.setContentView(mDialogView);
        }
        return this;
    }

    /**
     * 设置是否可以取消
     *
     * @param cancel
     * @return
     */
    public PermissionDialog setCancelable(boolean cancel) {
        if (null != mDialog) {
            mDialog.setCancelable(cancel);
        }
        return this;
    }

    /**
     * 设置对话框关闭监听
     *
     * @param dismissListener
     * @return
     */
    public PermissionDialog setDismissListener(DialogInterface.OnDismissListener dismissListener) {
        if (null != mDialog) {
            mDialog.setOnDismissListener(dismissListener);
        }
        return this;
    }

    /**
     * 对话框view绑定事件
     *
     * @param viewListener
     * @return
     */
    public PermissionDialog setDialogViewListener(ViewListener viewListener) {
        if (null != viewListener && null != mDialogView) {
            viewListener.onBindView(mDialogView, this);
        }
        return this;
    }

    /**
     * 显示对话框
     */
    public void show() {
        if (null == mDialog) {
            return;
        }
        if (isShowing()) {
            return;
        }
        if (null == mContextWeakReference || null == mContextWeakReference.get()) {
            return;
        }
        if (mContextWeakReference.get() instanceof Activity) {
            try {
                mDialog.show();
            } catch (Exception e) {
            }
        }
    }

    /**
     * 隐藏对话框
     */
    public void dismiss() {
        if (null == mDialog) {
            return;
        }
        if (null == mContextWeakReference || null == mContextWeakReference.get()) {
            return;
        }
        if (isShowing()) {
            try {
                mDialog.dismiss();
                mDialog = null;
            } catch (Exception e) {
            }
        }
    }

    /**
     * 获取当前的dialog
     *
     * @return
     */
    public Dialog getDialog() {
        return mDialog;
    }

    /**
     * 判断对话框是否在显示
     *
     * @return
     */
    public boolean isShowing() {
        return null != mDialog && mDialog.isShowing();
    }

    /**
     * view事件监听器
     */
    public interface ViewListener {
        void onBindView(View v , PermissionDialog baseDialog);
    }

}
